import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
// import 'package:flutter_bugly/flutter_bugly.dart';

class ChannelPage extends StatefulWidget {
  final String title;

  @override
  const ChannelPage({super.key, required this.title});

  _ChannelPageState createState() => _ChannelPageState();
}

class _ChannelPageState extends State<ChannelPage> {
  Uint8List? bytesData;
  BasicMessageChannel<Object?> channel = SystemChannels.keyEvent;
  var result;
  FocusNode focusNode = FocusNode();
  bool hasFocus = false;

  @override
  void initState() {
    super.initState();
    Timer(Duration(seconds: 1), () {
      channel.setMessageHandler((message) => _handleMessage(message));
    });
  }

  Future<void> _handleMessage(Object? value) async {
    setState(() {
      print('${StackTrace.current}');
      result = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: SingleChildScrollView(
            child: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              TextField(
                focusNode: focusNode,
              ),
              TextField(),
              ElevatedButton(
                  onPressed: () {
                    focusNode..unfocus();
                  },
                  child: Text('normal')),
              Text(
                'Native侧按键返回结果值：${result}',
                style: TextStyle(fontSize: 16),
              ),
              ElevatedButton(
                  onPressed: () async {
                    await showDialog(
                        context: context,
                        builder: (BuildContext context) {
                          return AlertDialog(
                            title: Text('alert Dialog'),
                            content: Text('this is an alert dialog'),
                            actions: [
                              TextButton(
                                  onPressed: () {
                                    Navigator.pop(context);
                                  },
                                  child: Text('OK'))
                            ],
                          );
                        });
                  },
                  child: Text('Show Dialog'))
            ],
          ),
        )));
  }
}
